package com.bls.cate.ui.activity.read_article;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.cate.R;
import com.bls.cate.adapter.ReadArticlePageAdapter;
import com.bls.cate.listener.ReadArticlePageChangeListener;
import com.bls.cate.model.ArticleListRes;
import com.bls.cate.utils.FrescoImageLoader;
import com.bls.cate.utils.XunFeiUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;

import java.util.ArrayList;
import java.util.List;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/2/7.
 */

public class ReadArticleActivity extends BaseActivity {

    private ViewPager vpImages;
    private TextView tvTitle, tvContent;
    private ImageView btnBack;
    private SimpleDraweeView ivHeader;
    private TextView tvName, btnMore, tvTime;

    private ReadArticleActivity mActivity;
    private ArticleListRes.ResultListDataBean articleDate;
    private List<ArticleListRes.ResultListDataBean.ArticleImageListBean> articleImageList = new ArrayList<>();
    private static final int Screen_Width = ScreenUtils.getScreenWidth();
    private int[] imgHeights;
    private int defaultVpHeight;  //viewPage默认高度
    private RelativeLayout.LayoutParams vpImagesLayoutParams;

    // 语音合成对象
    private SpeechSynthesizer mTts;
    //讯飞语音工具类
    private XunFeiUtils xunFeiUtils;
    //播放状态 0：未开始 1：播放 2：暂停 3：暂停回复
    private int speechPlayState = 0;

    @Override
    protected void initParams() {
        mActivity = (ReadArticleActivity) getActivity();
        Intent intent = getIntent();
        articleDate = (ArticleListRes.ResultListDataBean) intent.getSerializableExtra("Article");

        //LogUtils.i(articleDate.getCreateTime());

        articleImageList = articleDate.getArticleImageList();

        imgHeights = new int[articleImageList.size()];
        for (int i = 0; i < articleImageList.size(); i++) {
            ArticleListRes.ResultListDataBean.ArticleImageListBean bean = articleImageList.get(i);
            double ratio = (double) bean.getWidth() / (double) bean.getHeight();
            int imageItemHeight = (int) (Screen_Width / ratio);
            imgHeights[i] = imageItemHeight;
        }
        initXunFeiSpeakParam();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_read_article;
    }

    @Override
    protected void initFvb() {
        btnBack = findViewById(R.id.btn_back);
        ivHeader = findViewById(R.id.iv_header);
        tvName = findViewById(R.id.tv_name);
        btnMore = findViewById(R.id.btn_more);
        vpImages = findViewById(R.id.vp_images);
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        tvTime = findViewById(R.id.tv_time);
    }

    @Override
    protected void initTitle() {
        FrescoImageLoader.showImage(ivHeader, articleDate.getHeaderImgSmall());
        tvName.setText(articleDate.getNickName());
    }

    @Override
    protected void initView() {
        defaultVpHeight = imgHeights[0];
        vpImagesLayoutParams = (RelativeLayout.LayoutParams) vpImages.getLayoutParams();
        vpImagesLayoutParams.height = defaultVpHeight;
        vpImages.setLayoutParams(vpImagesLayoutParams);
        ReadArticlePageAdapter readArticlePageAdapter = new ReadArticlePageAdapter(mActivity, articleImageList);
        vpImages.setAdapter(readArticlePageAdapter);
        tvTime.setText(articleDate.getCreateTime());
    }

    @Override
    protected void initListener() {
        setOnClickListener(btnBack, btnMore);
        ReadArticlePageChangeListener readArticlePageChangeListener = new ReadArticlePageChangeListener(vpImages, vpImagesLayoutParams, defaultVpHeight, imgHeights);
        vpImages.addOnPageChangeListener(readArticlePageChangeListener);
    }

    @Override
    protected void initData() {
        //设置文章内容对其方式
        tvContent.setGravity(articleDate.getArticleAlign() == 0 ? (Gravity.START | Gravity.TOP) : (articleDate.getArticleAlign() == 1 ? (Gravity.CENTER_HORIZONTAL | Gravity.TOP) : (Gravity.END | Gravity.TOP)));
        tvTitle.setText(articleDate.getArticleTitle());
        tvContent.setText(articleDate.getArticleContent());
    }

    private void initXunFeiSpeakParam() {
        xunFeiUtils = new XunFeiUtils(mActivity);
        mTts = xunFeiUtils.initSpeechSynthesizer(new XunFeiUtils.InitListenerCallback() {
            @Override
            public void onInitCallback(int code) {
                if (code != ErrorCode.SUCCESS) {
                    LogUtils.i("初始化失败,错误码：" + code);
                }
            }
        });
    }

    @Override
    protected void initOnPause() {
        super.initOnPause();
        mTts.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                mActivity.finish();
                break;
            case R.id.btn_more:
                if (speechPlayState == 0) {
                    speechPlayState = 1;
                } else if (speechPlayState == 1) {
                    mTts.pauseSpeaking();
                    speechPlayState = 2;
                    return;
                } else if (speechPlayState == 2) {
                    mTts.resumeSpeaking();
                    speechPlayState = 1;
                    return;
                }
                xunFeiUtils.startSpeech(mTts, articleDate.getArticleContent(), new XunFeiUtils.SynthesizerListenerCallback() {
                    @Override
                    public void onSpeakBegin() {
                        LogUtils.i("开始播放");
                    }

                    @Override
                    public void onSpeakPaused() {
                        LogUtils.i("暂停播放");
                    }

                    @Override
                    public void onSpeakResumed() {
                        LogUtils.i("继续播放");
                    }

                    @Override
                    public void onCompleted(SpeechError speechError) {
                        if (speechError == null) {
                            speechPlayState = 0;
                            LogUtils.i("播放完成");
                        } else {
                            LogUtils.i(speechError.getPlainDescription(true));
                        }
                    }

                    @Override
                    public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
                    }

                    @Override
                    public void onSpeakProgress(int percent, int beginPos, int endPos) {
                    }
                });
                break;
        }
    }
}
